import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";

// 协议配置数据结构定义
export interface ProtocolConfigField {
  /** 唯一标识 */
  id: number;

  /** 协议名称 */
  protocolName: string;

  /** 帧头标识 */
  frameHeader?: string;

  /** 帧长度 */
  frameLength?: number;

  /** 校验起始位置 */
  checkStart?: number;

  /** 校验结束位置 */
  checkEnd?: number;

  /** 校检帧尾类型 */
  checkTail?: string;

  /** 轮询指令内容 */
  pollingInstruction?: string;

  /** 记录创建时间 */
  createdAt?: string | Date;

  /** 记录最后更新时间 */
  updatedAt?: string | Date;
}

export const createProtocolConfigField = (
  tableAction: TableAction<ProtocolConfigField> = {}
): Fields[] => {
  return [
    {
      valueType: "number",
      inputType: "input",
      key: "id",
      title: "ID",
      disabled: true,
      dataIndex: "id",
      width: 80,
      isEdit: false,
      visible: true,
    },
    {
      valueType: "string",
      inputType: "input",
      title: "协议名称",
      dataIndex: "protocolName",
      key: "protocolName",
      placeholder: "请输入协议名称",
      rules: [
        { required: true, message: "协议名称不能为空" },
        { max: 255, message: "协议名称长度不能超过255字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "帧头标识",
      dataIndex: "frameHeader",
      key: "frameHeader",
      placeholder: "请输入帧头标识",
      rules: [
        { required: false },
        { max: 255, message: "帧头标识长度不能超过255字符" },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "帧长度",
      dataIndex: "frameLength",
      key: "frameLength",
      placeholder: "请输入帧长度",
      rules: [
        { required: false },
        {
          type: "number",
          min: 0,
          max: 2147483647,
          message: "帧长度必须在0-2147483647范围内",
        },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "校验起始位置",
      dataIndex: "checkStart",
      key: "checkStart",
      placeholder: "请输入校验起始位置",
      rules: [
        { required: false },
        {
          type: "number",
          min: 0,
          max: 2147483647,
          message: "校验起始位置必须在0-2147483647范围内",
        },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "校验结束位置",
      dataIndex: "checkEnd",
      key: "checkEnd",
      placeholder: "请输入校验结束位置",
      rules: [
        { required: false },
        {
          type: "number",
          min: 0,
          max: 2147483647,
          message: "校验结束位置必须在0-2147483647范围内",
        },
      ],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "校检帧尾类型",
      dataIndex: "checkTail",
      key: "checkTail",
      placeholder: "请选择校检帧尾类型",
      initialValue: "CRC16",
      options: [
        { value: "CRC16", title: "CRC16" },
        { value: "CRC32", title: "CRC32" },
        { value: "CHECKSUM", title: "CHECKSUM" },
        { value: "LRC", title: "LRC" },
        // 可根据实际情况添加更多选项
      ],
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "轮询指令内容",
      dataIndex: "pollingInstruction",
      key: "pollingInstruction",
      placeholder: "请输入轮询指令内容",
      autoSize: { minRows: 3, maxRows: 6 },
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "创建时间",
      dataIndex: "createdAt",
      key: "createdAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "更新时间",
      dataIndex: "updatedAt",
      key: "updatedAt",
      disabled: true,
      visible: false,
      rules: [{ required: false }],
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 200,
      render: (_: any, record: ProtocolConfigField) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除此协议配置？"
            description="删除配置将无法恢复"
            onConfirm={() => tableAction.delete?.([record.id])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
};
